package ru.st.selenium.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.*;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import ru.st.selenium.model.Film;

public class InternalPage extends AnyPage {

	public InternalPage(PageManager pages) {
		super(pages);
	}

	public InternalPage ensurePageLoaded() {
		super.ensurePageLoaded();
		wait.until(presenceOfElementLocated(By.cssSelector("nav")));
		return this;
	}

	@FindBy(css = "nav a[href $= '?go=profile']")
	private WebElement userProfileLink;

	@FindBy(css = "nav a[href $= '?go=users']")
	private WebElement userManagementLink;

	@FindBy(css = "nav a[onclick $= '?logout']")
	private WebElement logoutLink;

	@FindBy(xpath = "//a[contains(@href,\"add\")]")
	private WebElement addmovie;

	@FindBy(xpath = "//input[@id='submit']")
	private WebElement submitaddmovie;

	public UserProfilePage clickUserProfilePage() {
		userProfileLink.click();
		return pages.userProfilePage;
	}

	public AnyPage submitAddMovie() {
		submitaddmovie.click();
		return pages.AnyPage;
	}

	public AnyPage clickAddMovieLink() {
		addmovie.click();
		return pages.AnyPage;
	}

	public AnyPage selectFilm(Film film) {
		String fullFilmPath = "//div[contains (@id, '" + film.getId() + "')]";
		driver.findElement(By.xpath(fullFilmPath)).click();
		return pages.AnyPage;
	}

	public UserManagementPage clickUserManagementLink() {
		userManagementLink.click();
		return pages.userManagementPage;
	}

	public LoginPage clickLogoutLink() {
		logoutLink.click();
		wait.until(alertIsPresent()).accept();
		return pages.loginPage;
	}

	public AnyPage searchReques(String title) {
//		driver.findElement(By.id("q")).clear();
		String search = title;
		driver.findElement(By.id("q")).sendKeys(search);
		driver.findElement(By.id("q")).sendKeys(Keys.RETURN);
		return pages.AnyPage;
	}
}
